package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.GameSupplyRule;
import com.sls.web.manager.game.pojo.GoodsManageInfo;
import com.sls.web.manager.game.querybean.GameSupplyRuleQuery;
import com.sls.web.manager.game.service.GameSupplyRuleService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

/**
 * 游戏供货规则Impl
 * 
 * @author gesx
 * 
 */
@Repository("gameSupplyRuleService")
public class GameSupplyRuleServiceImpl extends SuperService implements
		GameSupplyRuleService {
	private static Logger log = Logger
			.getLogger(GameSupplyRuleServiceImpl.class);
	private Gson gson;

	public GameSupplyRuleServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(GameSupplyRule gameSupplyRule, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GameSupplyRuleServiceImpl save method ....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SUPPLY_RULE_SAVE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gameSupplyRule.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gameSupplyRule);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GameSupplyRuleServiceImpl save method ....");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean update(GameSupplyRule gameSupplyRule, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GameSupplyRuleServiceImpl update method ....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SUPPLY_RULE_UPDATE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		gameSupplyRule.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, gameSupplyRule);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GameSupplyRuleServiceImpl update method ....");
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public GameSupplyRule queryById(String id, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GameSupplyRuleServiceImpl queryByGoodsId method ....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SUPPLY_RULE_QUERY_BY_GOODSID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		GameSupplyRule game = new GameSupplyRule();
		game.setId(id);
		game.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, game);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			String body = dto.getBody();
			GameSupplyRule game2 = gson.fromJson(body, GameSupplyRule.class);
			log.info("当前用户："
					+ userInfoVO.getUserId()
					+ ",Leave GameSupplyRuleServiceImpl queryByGoodsId method ....");
			return game2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<GameSupplyRule> queryList(
			GameSupplyRuleQuery gameSupplyRuleQuery, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ ",Enter GameSupplyRuleServiceImpl queryList method ....");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.GAME_SUPPLY_RULE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, gameSupplyRuleQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			Pagination<GameSupplyRule> game2 = gson.fromJson(dto.getBody(),
					new TypeToken<Pagination<GameSupplyRule>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ ",Leave GameSupplyRuleServiceImpl queryList method ....");
			return game2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<GoodsManageInfo> goodsNameList(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_GOODSNAME_LIST);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务器返回信息：" + dto.getBody());
			List<GoodsManageInfo> goods = gson.fromJson(dto.getBody(),
					new TypeToken<List<GoodsManageInfo>>() {
					}.getType());
			return goods;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		GameSupplyRuleServiceImpl.log = log;
	}

	public Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}

}
